package bb.lanxing.mvp.presetner;

import android.content.DialogInterface;
import bb.lanxing.R;
import bb.lanxing.model.database.RouteBook;
import bb.lanxing.model.database.RouteBookPoint;
import bb.lanxing.model.database.RouteSlope;
import bb.lanxing.mvp.model.AltitudeModelImpl;
import bb.lanxing.mvp.model.RouteSlopeModelImpl;
import bb.lanxing.mvp.model.i.IAltitudeModel;
import bb.lanxing.mvp.model.i.IRouteSlopeModel;
import bb.lanxing.mvp.presetner.i.IRouteChartPresenter;
import bb.lanxing.mvp.view.i.IRouteChartView;
import bb.lanxing.util.DistanceUtil;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class RouteBookChartPresenterImpl extends BasePresenter implements IRouteChartPresenter {
    public static final int ROUTE_POINT_COUNT = 1053;
    private IRouteChartView chartView;
    private List<RouteBookPoint> lushuPoints;
    private IRouteSlopeModel slopeModel = new RouteSlopeModelImpl();
    private IAltitudeModel altitudeModel = new AltitudeModelImpl();

    public RouteBookChartPresenterImpl(IRouteChartView chartView) {
        this.chartView = chartView;
    }

    @Override
    public void loadChart(final long routeId) {
        final Subscription subscribe = this.altitudeModel.requestLushuAltitudePoint(routeId, ROUTE_POINT_COUNT)
                .observeOn(Schedulers.computation()).flatMap(new Func1<List<RouteBookPoint>, Observable<List<Float>>>() {
            @Override
            public Observable<List<Float>> call(List<RouteBookPoint> lushuPoints) {
                RouteBookChartPresenterImpl.this.lushuPoints = lushuPoints;
                RouteBook byServerId = RouteBook.getByServerId(routeId);
                if (byServerId == null) {
                    return Observable.error(new IllegalArgumentException("Error route server id#" + routeId));
                }
                int size = lushuPoints.size();
                ArrayList arrayList = new ArrayList(size);
                float f = 0.0f;
                arrayList.add(0.0f);
                for (int i = 1; i < size; i++) {
                    RouteBookPoint lushuPoint = lushuPoints.get(i - 1);
                    RouteBookPoint lushuPoint2 = lushuPoints.get(i);
                    f = (float) (f + DistanceUtil.get(lushuPoint.getLatitude(), lushuPoint.getLongitude(), lushuPoint2.getLatitude(), lushuPoint2.getLongitude()));
                    arrayList.add(f);
                }
                float distance = (float) (byServerId.getDistance() / f);
                for (int i2 = 0; i2 < arrayList.size(); i2++) {
                    arrayList.set(i2, (Float) arrayList.get(i2) * distance);
                }
                return Observable.just(arrayList);
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<>() {
            @Override
            public void onCompleted() {
                RouteBookChartPresenterImpl.this.chartView.dismissLoadingDialog();
            }

            @Override
            public void onError(Throwable e) {
                RouteBookChartPresenterImpl.this.chartView.dismissLoadingDialog();
                e.printStackTrace();
            }

            @Override
            public void onNext(List<Float> distances) {
                RouteBookChartPresenterImpl.this.chartView.onLoadChart(RouteBookChartPresenterImpl.this.lushuPoints, distances);
            }
        });
        addSubscription(subscribe);
        this.chartView.showLoadingDialog(R.string.dialog_loading, new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                RouteBookChartPresenterImpl.this.cancelSubscription(subscribe);
                RouteBookChartPresenterImpl.this.chartView.exit();
            }
        });
    }

    @Override
    public void loadSlope(long routeId) {
        addSubscription(this.slopeModel.requestRouteSlop(routeId, ROUTE_POINT_COUNT)
                .observeOn(AndroidSchedulers.mainThread()).subscribe((Subscriber<? super List<RouteSlope>>) new Subscriber<List<RouteSlope>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(List<RouteSlope> slopes) {
                RouteBookChartPresenterImpl.this.chartView.onLoadSlope(slopes);
            }
        }));
    }
}
